package com.ruoyi.project.bill.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.project.bill.domain.*;
import com.ruoyi.project.bill.mapper.BillDefineMapper;
import com.ruoyi.project.bill.mapper.BillFormMapper;
import com.ruoyi.project.bill.mapper.BillFormulaMapper;
import com.ruoyi.project.bill.mapper.BillListenerMapper;
import com.ruoyi.project.bill.service.IBillActionService;
import com.ruoyi.project.bill.service.IBillDefineService;

import lombok.extern.slf4j.Slf4j;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 单据定义Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-03
 */
@Service
@Slf4j
public class BillDefineServiceImpl extends ServiceImpl<BillDefineMapper, BillDefine> implements IBillDefineService
{
    @Autowired
    private BillDefineMapper billDefineMapper;

    @Autowired
    private IBillActionService iBillActionService;

    @Autowired
    private BillFormulaMapper billFormulaMapper;

    @Autowired
    private BillFormMapper billFormMapper;

    @Autowired
    private BillListenerMapper billListenerMapper;


    /**
     * 查询单据定义
     * 
     * @param id 单据定义主键
     * @return 单据定义
     */
    @Override
    public BillDefine selectBillDefineById(String id)
    {
        return billDefineMapper.selectBillDefineById(id);
    }

    /**
     * 批量查询ids
     */
    @Override
    public List<BillDefine> selectBillDefineByIds(String[] ids)
    {
        return billDefineMapper.selectBillDefineByIds(ids);
    }

    /**
     * 查询单据定义列表
     * 
     * @param billDefine 单据定义
     * @return 单据定义
     */
    @Override
    public List<BillDefine> selectBillDefineList(BillDefine billDefine)
    {
        return billDefineMapper.selectBillDefineList(billDefine);
    }

    /**
     * 新增单据定义
     * 
     * @param billDefine 单据定义
     * @return 结果
     */
    @Override
    public String insertBillDefine(BillDefine billDefine)
    {
        String billDefineId = UUID.fastUUID().toString();
        billDefine.setId(billDefineId);
        int code1= billDefineMapper.insert(billDefine);
        //插入默认保存，提交，动作
        BillAction savebillAction = new BillAction();
        String savebillActionId = UUID.fastUUID().toString();
        savebillAction.setId(savebillActionId);
        savebillAction.setActionName("保存");
        savebillAction.setActionCode("save");
        savebillAction.setBillDefine(billDefineId);
        iBillActionService.insertBillAction(savebillAction);

        BillAction submitbillAction = new BillAction();
        String submitbillActionId = UUID.fastUUID().toString();
        submitbillAction.setId(submitbillActionId);
        submitbillAction.setActionName("提交");
        submitbillAction.setActionCode("submit");
        submitbillAction.setBillDefine(billDefineId);
        iBillActionService.insertBillAction(submitbillAction);

        BillAction auditbillAction = new BillAction();
        String auditbillActionId = UUID.fastUUID().toString();
        auditbillAction.setId(auditbillActionId);
        auditbillAction.setActionName("审核");
        auditbillAction.setActionCode("audit");
        auditbillAction.setBillDefine(billDefineId);
        iBillActionService.insertBillAction(auditbillAction);

        BillAction logicDeletebillAction = new BillAction();
        String logicDeletebillActionId = UUID.fastUUID().toString();
        logicDeletebillAction.setId(logicDeletebillActionId);
        logicDeletebillAction.setActionName("逻辑删除");
        logicDeletebillAction.setActionCode("logicDelete");
        logicDeletebillAction.setBillDefine(billDefineId);
        iBillActionService.insertBillAction(logicDeletebillAction);

        BillListener billListener = new BillListener();
        String billListenerId = UUID.fastUUID().toString();
        billListener.setId(billListenerId);
        billListener.setBillDefine(billDefineId);
        billListener.setBillActionId(logicDeletebillActionId);
        billListener.setListenerName("逻辑删除通用监听");
        billListener.setListenerCode("逻辑删除通用监听");
        billListenerMapper.insert(billListener);
        return code1>0? null:"保存失败";
    }

    /**
     * 修改单据定义
     * 
     * @param billDefine 单据定义
     * @return 结果
     */
    @Override
    public String updateBillDefine(BillDefine billDefine)
    {

        int code1= billDefineMapper.updateById(billDefine);
        return code1>0? null:"保存失败";
    }

    /**
     * 批量删除单据定义
     * 
     * @param ids 需要删除的单据定义主键
     * @return 结果
     */
    @Override
    public String deleteBillDefineByIds(String[] ids)
    {
        int code = billDefineMapper.deleteBatchIds(Collections.arrayToList(ids));
        return code>0?null:"删除失败";
    }

    /**
     * 删除单据定义信息
     * 
     * @param id 单据定义主键
     * @return 结果
     */
    @Override
    public String deleteBillDefineById(String id)
    {
        int code= billDefineMapper.deleteById(id);
        return code>0?null:"删除失败";
    }

    @Override
    public BillDefine getBillDefineByDefineName(String defineName) {
        BillDefine billDefine = billDefineMapper.selectOne(new LambdaQueryWrapper<BillDefine>().eq(BillDefine::getDefineCode, defineName));
        //获取单据动作列表
        List<BillAction> billActions = iBillActionService.getBillActionByBillDefineId(billDefine.getId());
        //获取单据表单
        List<BillForm> billForms =  billFormMapper.getBillFormByBillDefineId(billDefine.getId());
        //获取单据公式
        List<BillFormula> billFormulas = billFormulaMapper.getBillFormulaByBillDefineId(billDefine.getId());
        billDefine.setBillActions(billActions);
        billDefine.setBillForms(billForms);
        billDefine.setBillFormulas(billFormulas);
        return billDefine;

    }

}
